package com.fone.player.activity.personal;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fone.player.R;
import com.fone.player.activity.BaseActivity;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.client.Rst;
import com.fone.player.client.UpdatepicRst;
import com.fone.player.client.VbRst;
import com.fone.player.constant.FoneConstant;
import com.fone.player.sns.ISNSLoginManager;
import com.fone.player.sns.ISNSLoginManager.AuthorizingStateListener;
import com.fone.player.sns.UserInfoManager;
import com.fone.player.sns.bean.SNSUserInfo;
import com.fone.player.sns.impl.SNSLoginManager;
import com.fone.player.sns.util.SNSUtil;
import com.fone.player.util.CropImageUtil;
import com.fone.player.util.CropImageUtil.CropImageListener;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.LoginUtil;
import com.fone.player.view.BindButton;
import com.fone.player.view.ChangeHeadImageDialog;
import com.fone.player.view.ChangeNickNameDialog;
import com.fone.player.view.LoginoutDialog;
import com.fone.player.view.ModifyPwdDialog;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

@SuppressLint("HandlerLeak")
public class UserInfoActivity extends BaseActivity implements OnClickListener,
		OnTouchListener {
	private static final String TAG = "UserInfoActivity";

	/* 标志位 */
	public static final int REQUEST_CAMERA = 0;
	public static final int REQUEST_GALLERY = 1;
	public static final int SNS_BIND_SUCCESS = 2;
	public static final int REMOVE_SNS_BIND_SUCCESS = 3;
	public static final int LOGINOUT_SUCCESS = 4;
	public static final int CHANGE_NICKNAME_SUCCESS = 5;
	public static final int CHANGE_PSW_SUCCESS = 6;
	public static final int CHANGE_GENDER_SUCCESS = 7;
	public static final int CHANGE_USERNAME_SUCCESS = 8;
	public static final int SHOW_WAITTING = 9;
	public static final int CANCEL_WAITTING = 10;

	private Context mContext;
	private TextView titleTextView;
	private Button leftBt, rightBt, mLogoutBt;
	private RelativeLayout user_head_rl, user_password_rl, user_name_rl, userAssetRl;
	private RelativeLayout user_vip_cost_record_rl, user_vip_charge_record_rl;
	private UserInfoActivity mUserInfoActivity = this;
	private ImageView mUserHeadIcon;

	private RadioGroup mGenderRg;
	private RadioButton mSecretRb, mMaleRb, mFemaleRb;

	private CropImageUtil corpImageUtil;
	// private Drawable userDrawable;// 用户更新的头像

	private TextView userIdTv, userNameTv, userAccountTv, userViptimeTv,
			userAssetTv;
	private BindButton sinaCb, tencentCb, QQCb;
	private LinearLayout waitting;

	private RelativeLayout sinaRl, tencentRl, qqRl;
	private View parting_line_1, parting_line_2;

	private List<SNSUserInfo> binds;
	private SNSUserInfo tempUser;
	private int tempGender = 3;
	private MyAuthorizingStateListener mAuthorizingStateListener;
	private SNSLoginManager mSNSLoginManager;
	public Handler mHandler;

	private boolean vbCanClick = false;// vb是否可点击请求

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = getApplicationContext();
		setContentView(R.layout.personal_user_info_view);

		initView();
		initHandler();
		iniData();
		corpImageUtil = CropImageUtil.getInstance(this);

		corpImageUtil.setCropImageListener(new CropImageListener() {

			@Override
			public void cropImageSuccess(Intent data, String path) {
				// 图片保存成功 更新界面
				L.v(TAG, "cropImageSuccess", "path : " + path);

				String headPicString = LoginUtil.getHeadPicString(path);
				L.v(TAG, "cropImageSuccess", "headPicString is null or '': "
						+ TextUtils.isEmpty(headPicString));
				if (TextUtils.isEmpty(headPicString)) {
					FoneUtil.showToast(mContext, "头像更换失败");
					return;
				}
				if (FoneUtil.isNetOkWithToast(mContext)) {
					waitting.setVisibility(View.VISIBLE);
					Request.getInstance().updatepic(1, headPicString,
							new UpdatepicCallBack());

					File mfile = new File(path);
					if (mfile.exists()) {
						Bitmap bm = BitmapFactory.decodeFile(path);
						mUserHeadIcon.setImageBitmap(bm);
					}
				}
			}

			@Override
			public void cropImageFaild() {
				// 图片保存失败 提示
				Toast.makeText(mContext, "头像更换失败", Toast.LENGTH_SHORT).show();
			}
		});
	}

	public void initHandler() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case REQUEST_CAMERA:
					corpImageUtil.getFromCamera();
					break;
				case REQUEST_GALLERY:
					corpImageUtil.getFromGallery();
					break;
				case SNS_BIND_SUCCESS:
					Integer bindType = (Integer) (msg.obj);
					switch (bindType) {
					case ISNSLoginManager.SINA_WEIBO_TYPE:
						sinaCb.setChecked(true);
						break;
					case ISNSLoginManager.TENCENT_WEIBO_TYPE:
						tencentCb.setChecked(true);
						break;
					case ISNSLoginManager.TENCENT_QQ_TYPE:
						QQCb.setChecked(true);
						break;
					}
					tempUser = null;
					FoneUtil.showToast(mContext, "绑定成功");
					waitting.setVisibility(View.GONE);
					break;
				case REMOVE_SNS_BIND_SUCCESS:
					Integer unbindType = (Integer) (msg.obj);
					switch (unbindType) {
					case ISNSLoginManager.SINA_WEIBO_TYPE:
						sinaCb.setChecked(false);
						break;
					case ISNSLoginManager.TENCENT_WEIBO_TYPE:
						tencentCb.setChecked(false);
						break;
					case ISNSLoginManager.TENCENT_QQ_TYPE:
						QQCb.setChecked(false);
						break;
					}
					tempUser = null;
					FoneUtil.showToast(mContext, "已解除绑定");
					waitting.setVisibility(View.GONE);
					break;
				case LOGINOUT_SUCCESS:
					// FoneUtil.showToast(mContext, "注销成功");
					waitting.setVisibility(View.GONE);
					finish();
					break;
				case CHANGE_NICKNAME_SUCCESS:
					FoneUtil.showToast(mContext, "昵称修改成功");
					userNameTv.setText((String) (msg.obj));
					waitting.setVisibility(View.GONE);
					break;
				case CHANGE_USERNAME_SUCCESS:
					FoneUtil.showToast(mContext, "用户名修改成功");
					userAccountTv.setText((String) (msg.obj));
					waitting.setVisibility(View.GONE);
					break;
				case CHANGE_PSW_SUCCESS:
					FoneUtil.showToast(mContext, "密码修改成功");
					waitting.setVisibility(View.GONE);
					break;
				case CHANGE_GENDER_SUCCESS:
					switch (tempGender) {
					case 0:// 男
						mGenderRg.check(R.id.user_male_rb);
						break;
					case 1:// 女
						mGenderRg.check(R.id.user_female_rb);
						break;
					case 2:// 保密
						mGenderRg.check(R.id.user_secret_rb);
						break;
					default:
						break;
					}
					tempGender = 3;
					FoneUtil.showToast(mContext, "性别修改成功");
					waitting.setVisibility(View.GONE);
					break;
				case SHOW_WAITTING:
					waitting.setVisibility(View.VISIBLE);
					break;
				case CANCEL_WAITTING:
					waitting.setVisibility(View.GONE);
					break;
				default:
					break;
				}
			}
		};

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case CropImageUtil.CAMERA_REQUEST_CODE:
			L.v("JasonZue", "返回值到了UserInfoActivity,CAMERA_REQUEST_CODE");

		case CropImageUtil.GALLERY_REQUEST_CODE:
			L.v("JasonZue", "返回值到了UserInfoActivity,GALLERY_REQUEST_CODE");
		case CropImageUtil.RESULT_REQUEST_CODE:
			L.v("JasonZue", "返回值到了UserInfoActivity,RESULT_REQUEST_CODE");
			CropImageUtil.getInstance(this).handleMyActivityResult(requestCode,
					resultCode, data);
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	private void initView() {
		leftBt = (Button) findViewById(R.id.common_title_left_bt);
		rightBt = (Button) findViewById(R.id.common_title_right_bt);
		titleTextView = (TextView) findViewById(R.id.common_title_text_tv);
		mLogoutBt = (Button) findViewById(R.id.user_logout_bt);
		waitting = (LinearLayout) findViewById(R.id.userinfo_waitting_ll);

		userIdTv = (TextView) findViewById(R.id.user_id_tv);
		userNameTv = (TextView) findViewById(R.id.user_name_tv);
		userAccountTv = (TextView) findViewById(R.id.user_account_tv);
		userViptimeTv = (TextView) findViewById(R.id.user_vip_time_tv);
		userAssetTv = (TextView) findViewById(R.id.user_asset_tv);

		sinaRl = (RelativeLayout) findViewById(R.id.user_account_sina_rl);
		tencentRl = (RelativeLayout) findViewById(R.id.user_account_tencent_rl);
		qqRl = (RelativeLayout) findViewById(R.id.user_account_tencent_qq_rl);
		userAssetRl = (RelativeLayout) findViewById(R.id.user_vip_asset_rl);
		parting_line_1 = findViewById(R.id.parting_line_1);
		parting_line_2 = findViewById(R.id.parting_line_2);
		userAssetRl.setOnClickListener(this);

		sinaCb = (BindButton) findViewById(R.id.bind_sina_check_box);
		tencentCb = (BindButton) findViewById(R.id.bind_tencent_check_box);
		QQCb = (BindButton) findViewById(R.id.bind_QQ_check_box);
		sinaCb.setOnClickListener(this);
		tencentCb.setOnClickListener(this);
		QQCb.setOnClickListener(this);

		leftBt.setText("个人信息");
		rightBt.setVisibility(View.GONE);
		titleTextView.setVisibility(View.GONE);

		mGenderRg = (RadioGroup) findViewById(R.id.user_gender_rg);
		mSecretRb = (RadioButton) findViewById(R.id.user_secret_rb);
		mMaleRb = (RadioButton) findViewById(R.id.user_male_rb);
		mFemaleRb = (RadioButton) findViewById(R.id.user_female_rb);
		mSecretRb.setOnTouchListener(this);
		mMaleRb.setOnTouchListener(this);
		mFemaleRb.setOnTouchListener(this);

		// 设置默认选中
		mGenderRg.check(R.id.user_secret_rb);
		user_head_rl = (RelativeLayout) findViewById(R.id.user_head_rl);
		user_head_rl.setOnClickListener(this);
		user_password_rl = (RelativeLayout) findViewById(R.id.user_password_rl);
		user_password_rl.setOnClickListener(this);
		user_name_rl = (RelativeLayout) findViewById(R.id.user_name_rl);
		user_name_rl.setOnClickListener(this);
//		user_account_rl = (RelativeLayout) findViewById(R.id.user_account_rl);
//		user_account_rl.setOnClickListener(this);
		mUserHeadIcon = (ImageView) findViewById(R.id.personal_user_info_head_iv);

		leftBt.setOnClickListener(this);
		mLogoutBt.setOnClickListener(this);

		user_vip_cost_record_rl = (RelativeLayout) findViewById(R.id.user_vip_cost_record_rl);
		user_vip_charge_record_rl = (RelativeLayout) findViewById(R.id.user_vip_charge_record_rl);
		user_vip_cost_record_rl.setOnClickListener(this);
		user_vip_charge_record_rl.setOnClickListener(this);
	}

	/**
	 * 初始化绑定button
	 */
	private void iniBindButtons(String type) {
		if ("sina".equals(type)) {
			sinaRl.setVisibility(View.GONE);
			parting_line_1.setVisibility(View.GONE);
		} else if ("tencent".equals(type)) {
			tencentRl.setVisibility(View.GONE);
			parting_line_2.setVisibility(View.GONE);
		} else if ("qq".equals(type)) {
			qqRl.setVisibility(View.GONE);
			parting_line_2.setVisibility(View.GONE);
		} 
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int gender = 3;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			switch (v.getId()) {
			case R.id.user_male_rb:
				if (mGenderRg.getCheckedRadioButtonId() != R.id.user_male_rb) {
					gender = 0;
				}
				break;
			case R.id.user_female_rb:
				if (mGenderRg.getCheckedRadioButtonId() != R.id.user_female_rb) {
					gender = 1;
				}
				break;
			case R.id.user_secret_rb:
				if (mGenderRg.getCheckedRadioButtonId() != R.id.user_secret_rb) {
					gender = 2;
				}
				break;
			default:
				break;
			}
			if (gender != 3 &&tempGender == 3&& FoneUtil.isNetOkWithToast(mContext)) {
				tempGender = gender;
				waitting.setVisibility(View.VISIBLE);
				Request.getInstance().updateuserinfo(0, 0, gender, "",
						new GenderChangeCallBack());
			}
		}
		return true;
	}

	private void iniData() {

		String headPicUrl = UserInfoManager.getUserHeadPic();
		L.v(TAG, "iniData"," headPicUrl : "+headPicUrl);
		if (!TextUtils.isEmpty(headPicUrl) && headPicUrl.startsWith("http://")) {
			
			DisplayImageOptions mOptions = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.my_head_default) // 设置图片在下载期间显示的图片
			.showImageForEmptyUri(R.drawable.my_head_default)// 设置图片Uri为空或是错误的时候显示的图片
			.showImageOnFail(R.drawable.my_head_default) // 设置图片加载/解码过程中错误时候显示的图片
			.cacheInMemory(true)// 设置下载的图片是否缓存在内存中
			.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
			.considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
			.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
			.bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
			.resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
			.build();// 构建完成
			
			ImageDownloadModule.getInstance().download(
					headPicUrl,
					mUserHeadIcon, mOptions);
		}
		userIdTv.setText(UserInfoManager.getUserDisplayId());
		userNameTv.setText(UserInfoManager.getUserNickName());
		userAccountTv.setText(UserInfoManager.getUserid());
		setUsername();
		setgender();
		setVipTime();
		iniBindsState();

		mAuthorizingStateListener = new MyAuthorizingStateListener();
		mSNSLoginManager = SNSLoginManager.getInstance();
		mSNSLoginManager.setAuthiringStateListener(mAuthorizingStateListener);
		// 请求vb
		if (FoneUtil.isNetOkWithToast(mContext)) {
			Request.getInstance().vb(new VbCallBack());
		}
	}

	/**
	 * 设置用户名
	 */
	private void setUsername() {
		String userType = UserInfoManager.getUserType();
		L.v(TAG, "setUsername", "userType : " + userType);
		iniBindButtons(userType);
		if ("sina".equals(userType)) {
			userAccountTv.setText("新浪微博用户");
		} else if ("tencent".equals(userType)) {
			userAccountTv.setText("腾讯微博用户");
		} else if ("qq".equals(userType)) {
			userAccountTv.setText("QQ用户");
		}else if ("wechat".equals(userType)) {
			userAccountTv.setText("微信用户");
		} else {
			userAccountTv.setText(UserInfoManager.getUserid());
		}
	}

	/**
	 * 设置性别
	 */
	private void setgender() {
		String genderString = UserInfoManager.getUserGender();
		
		L.v(TAG, "setgender", genderString+"");
		int gender = 2;
		if (!TextUtils.isEmpty(genderString)) {
			try {
				gender = Integer.valueOf(genderString);
			} catch (Exception e) {
				L.e(TAG, "setgender", e.getMessage());
				gender = 2;
			}
		}else{
			L.v(TAG, "setgender", "genderString is null !!!! ");
		}
		switch (gender) {
		case 0:
			mGenderRg.check(R.id.user_male_rb);
			break;
		case 1:
			mGenderRg.check(R.id.user_female_rb);
			break;
		case 2:
			mGenderRg.check(R.id.user_secret_rb);
			break;
		}
	}

	/**
	 * 设置vip的到期时间
	 */
	@SuppressLint("SimpleDateFormat")
	private void setVipTime() {
		String userFeeExpirestime = UserInfoManager.getUserFeeExpirestime();
		if (UserInfoManager.isVip()) {
			if(!TextUtils.isEmpty(userFeeExpirestime)){
				try {
					Long time = Long.valueOf(userFeeExpirestime);
					SimpleDateFormat dateFm = new SimpleDateFormat("yyyy年MM月dd日");
					userViptimeTv.setText(dateFm.format(new Date(time)));
				} catch (Exception e) {
					userViptimeTv.setText("已开通");
				}
			}else{
				userViptimeTv.setText("已开通");
			}
		} else {
			userViptimeTv.setText("未开通");
		}
	}

	/**
	 * 初始化绑定状态
	 */
	private void iniBindsState() {
		binds = UserInfoManager.getBinds();
		if (binds != null) {
			for (SNSUserInfo snsUserInfo : binds) {
				switch (snsUserInfo.snsType) {
				case ISNSLoginManager.SINA_WEIBO_TYPE:
					sinaCb.setChecked(true);
					break;
				case ISNSLoginManager.TENCENT_WEIBO_TYPE:
					tencentCb.setChecked(true);
					break;
				case ISNSLoginManager.TENCENT_QQ_TYPE:
					QQCb.setChecked(true);
					break;
				default:
					break;
				}
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.user_name_rl:

			if (FoneUtil.isNetOkWithToast(mContext)) {
				new ChangeNickNameDialog(mContext, mUserInfoActivity);
			}
			break;
		case R.id.user_head_rl:
			if (FoneUtil.isNetOkWithToast(mContext)) {

				new ChangeHeadImageDialog(mContext, mUserInfoActivity, mHandler);
			}
			break;

		case R.id.user_password_rl:
			if (FoneUtil.isNetOkWithToast(mContext)) {
				if ("100tv".equals(UserInfoManager.getUserType())) {
					new ModifyPwdDialog(mContext, mUserInfoActivity);
				} else {
					FoneUtil.showToast(mContext, "不能修改密码");
				}
			}
			break;
		case R.id.user_logout_bt:
			if (FoneUtil.isNetOkWithToast(mContext)) {
				new LoginoutDialog(mContext, mUserInfoActivity);
			}
			break;
		case R.id.common_title_left_bt:
			// 返回主界面
			finish();
			break;
		case R.id.bind_sina_check_box:
			// 新浪微博
			changeBindState(ISNSLoginManager.SINA_WEIBO_TYPE);
			break;
		case R.id.bind_tencent_check_box:
			// 腾讯微博
			changeBindState(ISNSLoginManager.TENCENT_WEIBO_TYPE);
			break;
		case R.id.bind_QQ_check_box:
			// 腾讯QQ
			changeBindState(ISNSLoginManager.TENCENT_QQ_TYPE);
			break;

		case R.id.user_vip_asset_rl:
			if (FoneUtil.isNetOkWithToast(mContext) && vbCanClick) {
				Request.getInstance().vb(new VbCallBack());
				vbCanClick = false;
			}
			break;
		case R.id.user_vip_cost_record_rl:// 消费记录
			if (FoneUtil.isNetOkWithToast(mContext)) {
				Intent consumeIntent = new Intent(this, RecordActivity.class);
				consumeIntent.putExtra(RecordActivity.RECORDTYPE,
						RecordActivity.CONSUME_RECORD);
				startActivity(consumeIntent);
			}
			break;
		case R.id.user_vip_charge_record_rl:// 充值记录
			if (FoneUtil.isNetOkWithToast(mContext)) {
				Intent chargeIntent = new Intent(this, RecordActivity.class);
				chargeIntent.putExtra(RecordActivity.RECORDTYPE,
						RecordActivity.CHARGE_RECORD);
				startActivity(chargeIntent);
			}
			break;
//		case R.id.user_account_rl:
//			if (FoneUtil.isNetOkWithToast(mContext)) {
//				if ("100tv".equals(UserInfoManager.getUserType())) {
//					if (UserInfoManager.isModified()) {
//						FoneUtil.showToast(mContext, "帐号不可修改");
//					} else {
//						new ChangeUserNameDialog(mContext, mUserInfoActivity);
//					}
//				}else {
//					FoneUtil.showToast(mContext, "帐号不可修改");
//				}
//			}
//			break;

		default:
			break;
		}
	}

	/**
	 * 改变绑定状态
	 * 
	 * @param Type
	 * 
	 */
	private void changeBindState(int type) {

		if (FoneUtil.isNetOkWithToast(mContext)) {
			BindButton temp;
			switch (type) {
			case ISNSLoginManager.SINA_WEIBO_TYPE:
				temp = sinaCb;
				break;
			case ISNSLoginManager.TENCENT_WEIBO_TYPE:
				temp = tencentCb;
				break;
			case ISNSLoginManager.TENCENT_QQ_TYPE:
				temp = QQCb;
				break;
			default:
				return;
			}

			if (temp.isChecked()) {
				waitting.setVisibility(View.VISIBLE);
				SNSUserInfo info = getBindsInfo(type);
				tempUser = info;
				Request.getInstance().binduser(0, 1,
						LoginUtil.getBinData(tempUser), new unBindCallBack());
			} else {
				mSNSLoginManager.authorizeByType(type);
			}
		}

	}

	/**
	 * 获取绑定用户信息
	 * 
	 * @param Type
	 * @return
	 */
	private SNSUserInfo getBindsInfo(int type) {
		if (binds != null && binds.size() != 0) {
			for (SNSUserInfo info : binds) {
				if (info.snsType == type) {
					return info;
				}
			}
		}
		return null;
	}

	/**
	 * 移除绑定用户信息
	 * 
	 * @param Type
	 * @return
	 */
	private void removeBindsInfo(int type) {
		if (binds != null && binds.size() != 0) {

			for (int i = 0; i < binds.size(); i++) {
				if (binds.get(i).snsType == type) {
					binds.remove(i);
					return;
				}
			}
		}
	}

	@Override
	public void distanceLoginCallback() {
		super.distanceLoginCallback();
		L.v(TAG, "distanceLoginCallback", "distance login !!!");
		finish();
	}

	/**
	 * 授权监听
	 * 
	 * @author user
	 * 
	 */
	private class MyAuthorizingStateListener implements
			AuthorizingStateListener {

		@Override
		public void authrizeAction(int action, String info) {
			switch (action) {
			case AuthorizingStateListener.AUTHORIZING:
				L.v(TAG, "authrizeAction", "AUTHORIZING");
				waitting.setVisibility(View.VISIBLE);
				break;
			case AuthorizingStateListener.AUTHORIZE_SUCCESS:
				L.v(TAG, "authrizeAction", "AUTHORIZE_SUCCESS");
				// Toast.makeText(UserInfoActivity.this, "授权成功",
				// Toast.LENGTH_SHORT).show();
				break;
			case AuthorizingStateListener.AUTHORIZE_CANCEL:
				L.v(TAG, "authrizeAction", "AUTHORIZE_CANCEL");
				waitting.setVisibility(View.GONE);
				break;
			case AuthorizingStateListener.AUTHORIZE_ERROR:
				L.v(TAG, "authrizeAction", "AUTHORIZE_ERROR");
				waitting.setVisibility(View.GONE);
				Toast.makeText(UserInfoActivity.this, info, Toast.LENGTH_SHORT)
						.show();
				break;
			default:
				break;
			}
		}

		@Override
		public void onUserInfoComplete(SNSUserInfo user) {
			L.v(TAG, "onUserInfoComplete", "userInfo : " + user);
			tempUser = user;
			Request.getInstance().binduser(1, 1, LoginUtil.getBinData(user),
					new BindCallback());
		}
	}

	/**
	 * 绑定回调
	 * 
	 * @author user
	 * 
	 */
	private class BindCallback implements Callback<Rst> {

		@Override
		public void onSuccess(Rst t) {
			L.v(TAG, "BindCallback onSuccess", t.toString());
			if (t.result != 0) {
				if (t.error != null) {
					try {
						switch (Integer.valueOf(t.error.errorcode)) {
						case 118:
							FoneUtil.showToast(mContext, "该帐号已被绑定");
							break;
						default:
							FoneUtil.showToast(mContext, "绑定失败");
							break;
						}
					} catch (Exception e) {
						FoneUtil.showToast(mContext, "绑定失败");
					}
					SNSLoginManager.getInstance().CancelAuthSNS(
							tempUser.snsType);
					waitting.setVisibility(View.GONE);
					return;
				}
			}
			new Thread() {
				@Override
				public void run() {
					if (binds == null) {
						binds = new ArrayList<SNSUserInfo>();
					}
					binds.add(tempUser);
					// 本地化绑定信息
					UserInfoManager.upBinds(tempUser);
					mHandler.obtainMessage(SNS_BIND_SUCCESS,
							Integer.valueOf(tempUser.snsType)).sendToTarget();
					FoneConstant.isLogin = false;
				};
			}.start();
		}

		@Override
		public void onFailure(Error error) {
			waitting.setVisibility(View.GONE);
			L.v(TAG,
					"BindCallback onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());

			if (error.isNetworkError()) {
				FoneUtil.showToast(mContext, "网络超时,请稍后重试");
				return;
			}
			FoneUtil.showToast(mContext, "绑定失败");
		}
	}

	/**
	 * 解绑回调
	 * 
	 * @author user
	 * 
	 */
	private class unBindCallBack implements Callback<Rst> {

		@Override
		public void onSuccess(Rst t) {
			L.v(TAG, "unBindCallBack onSuccess", t.toString());
			if (t.result != 0) {
				if (t.error != null) {
					FoneUtil.showToast(mContext, "解绑失败");
					waitting.setVisibility(View.GONE);
					return;
				}
			}
			new Thread() {
				@Override
				public void run() {
					removeBindsInfo(tempUser.snsType);
					UserInfoManager.removeBind(tempUser.snsType);
					mHandler.obtainMessage(REMOVE_SNS_BIND_SUCCESS,
							Integer.valueOf(tempUser.snsType)).sendToTarget();
					FoneConstant.isLogin = false;
				};
			}.start();
		}

		@Override
		public void onFailure(Error error) {
			waitting.setVisibility(View.GONE);
			L.v(TAG,
					"unBindCallBack onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());

			if (error.isNetworkError()) {
				FoneUtil.showToast(mContext, "网络超时,请稍后重试");
				return;
			}
			FoneUtil.showToast(mContext, "解绑失败");
		}
	}

	/**
	 * 性别修改回调
	 * 
	 * @author user
	 * 
	 */
	private class GenderChangeCallBack implements Callback<Rst> {

		@Override
		public void onSuccess(Rst t) {
			L.v(TAG, "GenderChangeCallBack onSuccess", t.toString());
			if (t.result != 0) {
				if (t.error != null) {
					tempGender = 3;
					FoneUtil.showToast(mContext, "修改失败");
					waitting.setVisibility(View.GONE);
					return;
				}
			}
			new Thread() {
				@Override
				public void run() {
					UserInfoManager.saveUserGender(String.valueOf(tempGender));
					mHandler.obtainMessage(CHANGE_GENDER_SUCCESS)
							.sendToTarget();
				};
			}.start();
		}

		@Override
		public void onFailure(Error error) {
			tempGender = 3;
			waitting.setVisibility(View.GONE);
			L.v(TAG, "GenderChangeCallBack onFailure", "isNetworkError : "
					+ error.isNetworkError() + " status : " + error.getStatus()
					+ " reason : " + error.getReason());

			if (error.isNetworkError()) {
				FoneUtil.showToast(mContext, "网络超时,请稍后重试");
				return;
			}
			FoneUtil.showToast(mContext, "修改失败");
		}
	}

	/**
	 * 请求vb回调
	 * 
	 * @author user
	 * 
	 */
	private class VbCallBack implements Callback<VbRst> {

		@Override
		public void onSuccess(VbRst t) {
			L.v(TAG, "VbCallBack onSuccess", t.toString());
			if (t.result != 0) {
				if (t.error != null) {
					userAssetTv.setText("点击查看");
					vbCanClick = true;
					return;
				}
			}
			if (!TextUtils.isEmpty(t.vb)) {
				// UserInfoManager.saveUserVbCount(t.vb);//更新本地用户vb数量
				userAssetTv.setText(t.vb);
				vbCanClick = false;
				userAssetRl.setClickable(vbCanClick);
			}
		}

		@Override
		public void onFailure(Error error) {
			L.v(TAG,
					"VbCallBack onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());

			userAssetTv.setText("点击查看");
			vbCanClick = true;
			if (error.isNetworkError()) {
				FoneUtil.showToast(mContext, "网络超时,请稍后重试");
				return;
			}
		}
	}

	/**
	 * 请求vb回调
	 * 
	 * @author user
	 * 
	 */
	private class UpdatepicCallBack implements Callback<UpdatepicRst> {

		@Override
		public void onSuccess(final UpdatepicRst t) {
			L.v(TAG, "UpdatepicCallBack onSuccess", t.toString());
			if (t.result != 0 || TextUtils.isEmpty(t.faceurl)) {
				if (t.error != null) {
					waitting.setVisibility(View.GONE);
					FoneUtil.showToast(mContext, "头像上传失败，请重试");
					return;
				}
			}
			FoneUtil.showToast(mContext, "头像上传成功");
			new Thread() {
				@Override
				public void run() {
					UserInfoManager.saveUserHeadPic(t.faceurl);
					mHandler.sendEmptyMessage(CANCEL_WAITTING);
				};
			}.start();
		}

		@Override
		public void onFailure(Error error) {
			waitting.setVisibility(View.GONE);
			L.v(TAG,
					"UpdatepicCallBack onFailure",
					"isNetworkError : " + error.isNetworkError() + " status : "
							+ error.getStatus() + " reason : "
							+ error.getReason());

			if (error.isNetworkError()) {
				FoneUtil.showToast(mContext, "网络超时,请稍后重试");
				return;
			}
			FoneUtil.showToast(mContext, "头像上传失败，请重试");
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (waitting.getVisibility() == View.VISIBLE) {
			waitting.setVisibility(View.VISIBLE);
			SNSUtil.stopSNS();
		}
	}

}
